package com.tolbahady.android.game;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class GameControllerActivity extends Activity {
	TabelGame game = TabelGame.startGame();
	private TextView player1Score;
	private Button btnPlayer1AddPoint;
	private Button btnPlayer2AddPoint;
	private TextView player2Score;
	public GameControllerActivity() {
		super();
		game = TabelGame.startGame();
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabel_game);
		
		getComponents();
		setActionListeners();
		update();
	}

	private void getComponents() {
		player1Score = (TextView)findViewById(R.id.txtPlayer1Score);
		player2Score = (TextView)findViewById(R.id.txtPlayer2Score);
		btnPlayer1AddPoint = (Button)findViewById(R.id.btnPlayer1AddPoint);
		btnPlayer2AddPoint = (Button)findViewById(R.id.btnPlayer2AddPoint);
	}
	private void setActionListeners() {
		btnPlayer1AddPoint.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				game.newPoint(0);
				update();
			}
		});
		btnPlayer2AddPoint.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				game.newPoint(1);
				update();
			}
		});
	}

	private void update() {
		if(game.isGameEnd()){
			((TextView)findViewById(R.id.winMsg)).setText(game.getWiner().getName() + " WIN!!");
		}
		int s1 = game.getPlayerScore(0);
		player1Score.setText(String.valueOf(s1));
		int s2 = game.getPlayerScore(1);
		player2Score.setText(String.valueOf(s2));
		if(game.getCurrentServer() == 0){
			((TextView)findViewById(R.id.txtPlayer1)).setText(game.getPlayers()[0].getName() + "(Serving)");
			((TextView)findViewById(R.id.txtPlayer2)).setText(game.getPlayers()[0].getName() + "         ");
		}
		else {
			((TextView)findViewById(R.id.txtPlayer1)).setText(game.getPlayers()[0].getName() + "         ");
			((TextView)findViewById(R.id.txtPlayer2)).setText(game.getPlayers()[0].getName() + "(Serving)");
		}
	}
	
	@Override
	protected void onStart() {
		super.onStart();
//		/update();
	}
}
